home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: delays: creating time delays with time.h (unix c)
- Date: Sat, 06 Apr 96 22:03:19 GMT
- Organization: none
- Message-ID: <828828199snz@genesis.demon.co.uk>
- References: <Pine.SUN.3.92.960404210632.28756A-100000@suntan>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <Pine.SUN.3.92.960404210632.28756A-100000@suntan>
- cdiaz@eng.usf.edu "CS" writes:
-
- >Hello! I'm trying to figure out if I can use the standard library (time.h)
- >to create a delay function where the program will pause for x amount of
- >time before continuing. 'x' I need it to be a value representing either
- >10th of a second or 100th of a second.
-
- The only functions the standard library gives you to read time values are
- time() and clock(). You get no guarantees as to their granularity, however.
- In many systems time() has a granularity of 1 second so doesn't fit
- your criteria. clock() doesn't measure wall-clock time but rather CPU time
- used. On a system without multi-tasking this may amount to the same thing
- but isn't in general.
-
- >Please, no references to using non-standard libraries: this code has to be
- >as platform independent as possible.
-
- I think you'll just have to accept that there is no single portable function
- that meets your requirements. I suggest you create your own delay function
- and use conditional compilation to compile code in it that is appropriate to
- the system e.g. using clock, nap, usleep, select, poll etc. as appropriate.
- One thing you definitely don't want to do is busy-wait on a multi-tasking
- system.
-
- >On that same note, suppose I have a variable getting the value from the
- >clock() function... how do I print the variable (of type clock_t or
- >time_t)? Thanks for your help!
-
- A clock_t value doesn't mean much by itself, the only quantity that has
- meaning is the difference between two such values. You can print the difference
- in seconds out using something like:
-
- clock_t start, end;
-
- start = clock();
-
- ...
-
- end = clock();
-
- printf("%Lf\n", ((long double)end - start) / CLOCKS_PER_SEC);
-
- For time_t there are various function to print it out formatted dates and
- times, e.g. ctime, gmtime, localtime, asctime, strftime. You can also use
- difftime to get the difference in seconds between 2 time_t values. The
- representation of time_t itself is unspecified so printing it out directly
- doesn't guarantee anything meaningful.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-